package com.ruoyi.web.controller.borrowing;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.BorrowingAmount;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IBorrowingAmountService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.BreakIterator;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 借款Controller
 * 
 * @author ruoyi
 * @date 2020-07-16
 */
@Controller
@RequestMapping("/ruoyi/amount")
public class BorrowingAmountController extends BaseController
{
    private String prefix = "ruoyi/amount";

    @Autowired
    private IBorrowingAmountService borrowingAmountService;
    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("ruoyi:amount:view")
    @GetMapping()
    public String amount()
    {
        return prefix + "/amount";
    }

    /**
     * 查询借款列表
     */
    @RequiresPermissions("ruoyi:amount:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BorrowingAmount borrowingAmount)
    {
        startPage();
        List<BorrowingAmount> list = borrowingAmountService.selectBorrowingAmountList(borrowingAmount);
        return getDataTable(list);
    }



    /**
     * 导出借款列表
     */
    @RequiresPermissions("ruoyi:amount:export")
    @Log(title = "借款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BorrowingAmount borrowingAmount)
    {
        List<BorrowingAmount> list = borrowingAmountService.selectBorrowingAmountList(borrowingAmount);
        ExcelUtil<BorrowingAmount> util = new ExcelUtil<BorrowingAmount>(BorrowingAmount.class);
        return util.exportExcel(list, "借款");
    }

    /**
     * 新增借款
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存借款
     */
    @RequiresPermissions("ruoyi:amount:add")
    @Log(title = "借款", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BorrowingAmount borrowingAmount)
    {
        //自动获取申请人姓名
        borrowingAmount.setbApplicant(ShiroUtils.getLoginName());
        //自动获取申请时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        return toAjax(borrowingAmountService.insertBorrowingAmount(borrowingAmount));
    }

    /**
     * 修改借款
     */
    @GetMapping("/edit/{bId}")
    public String edit(@PathVariable("bId") Long bId, ModelMap mmap)
    {
        BorrowingAmount borrowingAmount = borrowingAmountService.selectBorrowingAmountById(bId);
        mmap.put("borrowingAmount", borrowingAmount);
        return prefix + "/edit";
    }

    /**
     * 修改保存借款
     */
    @RequiresPermissions("ruoyi:amount:edit")
    @Log(title = "借款", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BorrowingAmount borrowingAmount)
    {
        return toAjax(borrowingAmountService.updateBorrowingAmount(borrowingAmount));
    }

    /**
     * 删除借款
     */
    @RequiresPermissions("ruoyi:amount:remove")
    @Log(title = "借款", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(borrowingAmountService.deleteBorrowingAmountByIds(ids));
    }


    /**
     * 提交申请
     */
    @Log(title = "催款申请业务", businessType = BusinessType.UPDATE)
    @PostMapping("/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long bId) {
        borrowingAmountService.submitExaStatus(bId);
        SysUser su = userService.selectUserByLoginName(ShiroUtils.getLoginName());
        List<BorrowingAmount> list  = borrowingAmountService.selectPriceByLoginName(ShiroUtils.getLoginName());
        if(su.getBalance().subtract(list.get(list.size()-1).getbAmount()).compareTo(BigDecimal.ZERO)==-1){
            return toAjax(0);
        }
        return success();
    }

    /**
     * 撤销申请
     */
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PostMapping( "/revocations")
    @ResponseBody
    public AjaxResult revocations(Long bId) {
        borrowingAmountService.revoExaStatus(bId);
        return success();
    }

    /**
     * 结束
     */
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PostMapping( "/over")
    @ResponseBody
    public AjaxResult over(Long bId) {
        borrowingAmountService.overExaStatus(bId);
        SysUser su = userService.selectUserByLoginName(ShiroUtils.getLoginName());
        List<BorrowingAmount> list  = borrowingAmountService.selectPriceByLoginName(ShiroUtils.getLoginName());
            su.setBalance(su.getBalance().subtract(list.get(list.size()-1).getbAmount()));
            userService.updateUser(su);
        return success();
    }
}

